<template>
  <van-icon :name="value ? 'star' : 'star-o'" :class="{ active: value }" @click="onCollect" />
</template>

<script>
import { addCollectAPI, deteleCollectAPI } from '@/api/User.js'
export default {
  name: 'CollectUser',
  props: {
    value: {
      type: Boolean,
      require: true
    },
    articleId: {
      type: [Number, String, Object],
      require: true
    }
  },
  data() {
    return {}
  },
  methods: {
    async onCollect() {
      try {
        if (this.value) {
          //  点赞收藏
          await addCollectAPI(this.articleId)
        } else {
          //  取消点赞收藏
          await deteleCollectAPI(this.articleId)
        }
        // 更新视图
        this.$emit('input', !this.value)
        this.$toast.success(!this.value ? '收藏成功！' : '取消收藏！')
      } catch (err) {
        this.$toast.fail('收藏失败！')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.active {
  color: red;
}
</style>
